package com.s_network.net;


import android.util.Log;

import com.google.gson.Gson;
import com.s7.network.exception.ERROR;
import com.s7.network.listener.OnRequestListener;
import com.s7.network.support.BaseObserver;
import com.s7.network.type.typeimpl.ParameterizedTypeImpl;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;

public class NetObserver<T> extends BaseObserver {

    public NetObserver() {
        super();
    }

    public NetObserver(OnRequestListener<T> listener) {
        super(listener);
    }

    @Override
    protected void onResponseBody(ResponseBody body) {
        try {
            String data = body.string();
            Log.d("请求结果", data);
            onSuccess(((Result<T>) new Gson().fromJson(data,
                    new ParameterizedTypeImpl(Result.class, getType()))).getData());
        } catch (Exception e) {
            e.printStackTrace();
           onUnusual(ERROR.PARSE_ERROR, "解析错误");
        }
    }
}
